package top.market.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import top.market.model.entity.Category;
import top.market.model.entity.Product;
import top.market.model.dto.QueryProductDTO;
import top.market.model.entity.ProductType;
import top.market.model.vo.ProductVo;
import top.market.model.vo.Result;
import top.market.service.ICategoryService;
import top.market.service.IProductService;
import top.market.service.IProductTypeService;
import top.market.service.IUserService;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author li yuhai
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    IProductService productService;

    @Autowired
    IProductTypeService productTypeService;

    @Autowired
    ICategoryService categoryService;

    // 根据name模糊查询
    @GetMapping("/search/name")
    public Result searchName(@RequestParam(value = "name") String name) {
        if (name == null || "".equals(name)) {
            return Result.error("名称不能为空");
        }
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Product::getName, name);
        List<Product> list = productService.list(queryWrapper);
        List<ProductVo> collect = list.stream().map(item -> {
            ProductVo productVo = new ProductVo();
            BeanUtils.copyProperties(item, productVo);
            LambdaQueryWrapper<Category> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Category::getId, item.getCategoryId());
            Category category = categoryService.getOne(queryWrapper1);
            productVo.setCategory(category);
            return productVo;
        }).collect(Collectors.toList());
        return Result.ok("查询成功", collect);
    }

    // 根据id查询商品
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable(value = "id") Long id) {
        // 获取product
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getId, id);
        Product product = productService.getOne(wrapper);

        ProductVo productVo = new ProductVo();
        BeanUtils.copyProperties(product, productVo);
        // 对应的类型品种
        LambdaQueryWrapper<ProductType> productTypeWrapper = new LambdaQueryWrapper<>();
        productTypeWrapper.eq(ProductType::getProductId, id);
        ProductType productType = productTypeService.getOne(productTypeWrapper);
        String jsonValue = productType.getValue();
        productVo.setProductType(jsonValue);
        productVo.setProductTypeObj(productType);
        return Result.ok("获取成功", productVo);
    }

    // 拿取这个List数据
    public List<String> getStrFromJsonStr(String json) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            List<String> list = objectMapper.readValue(json, List.class);
            System.out.println(list); // 输出: [细支, 中支, 粗支]
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
       return null;
    }

    @GetMapping("/list")
    public Result getProducts(@RequestParam(name = "categoryId") Long id ) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getCategoryId, id);
        List<Product> list = productService.list(wrapper);
        return Result.ok("获取成功", list);
    }

//    // 根据name、cate_id、remaining_number、price、status来查询
//    @GetMapping("/list")
//    public Result getProducts(@RequestBody QueryProductDTO productDTO) {
//        Product product = new Product();
//        BeanUtils.copyProperties(productDTO, product);
//        Page<Product> page = new Page<>(productDTO.getPageNum(), productDTO.getPageSize());
//        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
//        if (StrUtil.isNotEmpty(product.getName())){
//            wrapper.like(Product::getName, product.getName());
//        }
//        if (product.getCategoryId()!= null && product.getCategoryId()!= 0){
//            wrapper.eq(Product::getCategoryId, product.getCategoryId());
//        }
//        if (product.getRemainingNumber()!=null && product.getRemainingNumber()!= 0){
//            wrapper.eq(Product::getRemainingNumber, product.getRemainingNumber());
//        }
//        if (product.getPrice()!=null && product.getPrice().compareTo(BigDecimal.ZERO)>0){
//            wrapper.ge(Product::getPrice, product.getPrice());
//        }
//        if (product.getStatus() !=null && product.getStatus() > -1){
//            wrapper.eq(Product::getStatus, product.getStatus());
//        }
//        Page<Product> productPage = productService.getBaseMapper().selectPage(page, wrapper);
//        return Result.ok("获取成功", productPage);
//    }

    @PostMapping("/save")
    public Result save(@RequestBody Product product) {
        productService.save(product);
        return Result.ok("插入成功");
    }

    @PutMapping("/update")
    public Result updateCategory(@RequestBody Product product) {
        productService.updateById(product);
        return Result.ok("更新成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteCategory(@PathVariable Integer id) {
        productService.removeById(id);
        return Result.ok("删除成功");
    }
}
